﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SiteMapEditor.Controls
{
    public partial class PrivilegeControl : UserControl, ISiteMapSavable
    {
        private Dictionary<string, string> collec;

        private string initialEntity = "";
        private bool initialPrivCreate = false;
        private bool initialPrivRead = false;
        private bool initialPrivWrite = false;
        private bool initialPrivDelete = false;
        private bool initialPrivAppend = false;
        private bool initialPrivAppendTo = false;
        private bool initialPrivShare = false;
        private bool initialPrivAssign = false;
        private bool initialPrivAll = false;
        private bool initialPrivAllowQuickCampaign = false;
        private bool initialPrivUseInternetMarketing = false;

        #region Delegates

        public delegate void SaveEventHandler(object sender, SaveEventArgs e);

        #endregion

        #region Event Handlers

        public event SaveEventHandler Saved;

        #endregion

        public PrivilegeControl()
        {
            InitializeComponent();

            this.collec = new Dictionary<string, string>();

            ToolTip tip = new ToolTip();
            tip.ToolTipTitle = "Information";
            tip.SetToolTip(this.txtPrivilegeEntity, "Specifies the name of the entity to check privileges for.");
        }

        public PrivilegeControl(Dictionary<string, string> collection):this()
        {
            this.collec = collection;

            this.FillControls();
        }

        private void FillControls()
        {
            this.txtPrivilegeEntity.Text = collec.ContainsKey("Entity") ? collec["Entity"] : "";
            this.chkPrivilegeAll.Checked = collec.ContainsKey("Privilege") ? collec["Privilege"].IndexOf("All") >= 0 : false;
            this.chkPrivilegeAllowQuickCampaign.Checked = collec.ContainsKey("Privilege") ? collec["Privilege"].IndexOf("AllowQuickCampaign") >= 0 : false;
            this.chkPrivilegeAppend.Checked = collec.ContainsKey("Privilege") ? collec["Privilege"].IndexOf("Append") >= 0 : false;
            this.chkPrivilegeAppendTo.Checked = collec.ContainsKey("Privilege") ? collec["Privilege"].IndexOf("AppendTo") >= 0 : false;
            this.chkPrivilegeAssign.Checked = collec.ContainsKey("Privilege") ? collec["Privilege"].IndexOf("Assign") >= 0 : false;
            this.chkPrivilegeCreate.Checked = collec.ContainsKey("Privilege") ? collec["Privilege"].IndexOf("Create") >= 0 : false;
            this.chkPrivilegeDelete.Checked = collec.ContainsKey("Privilege") ? collec["Privilege"].IndexOf("Delete") >= 0 : false;
            this.chkPrivilegeRead.Checked = collec.ContainsKey("Privilege") ? collec["Privilege"].IndexOf("Read") >= 0 : false;
            this.chkPrivilegeShare.Checked = collec.ContainsKey("Privilege") ? collec["Privilege"].IndexOf("Share") >= 0 : false;
            this.chkPrivilegeUseInternetMarketing.Checked = collec.ContainsKey("Privilege") ? collec["Privilege"].IndexOf("UseInternetMarketing") >= 0 : false;
            this.chkPrivilegeWrite.Checked = collec.ContainsKey("Privilege") ? collec["Privilege"].IndexOf("Write") >= 0 : false;

            this.initialEntity = this.txtPrivilegeEntity.Text;
            this.initialPrivCreate = this.chkPrivilegeCreate.Checked;
            this.initialPrivRead = this.chkPrivilegeRead.Checked;
            this.initialPrivWrite = this.chkPrivilegeWrite.Checked;
            this.initialPrivDelete = this.chkPrivilegeDelete.Checked;
            this.initialPrivAppend = this.chkPrivilegeAppend.Checked;
            this.initialPrivAppendTo = this.chkPrivilegeAppendTo.Checked;
            this.initialPrivShare = this.chkPrivilegeShare.Checked;
            this.initialPrivAssign = this.chkPrivilegeAssign.Checked;
            this.initialPrivAll = this.chkPrivilegeAll.Checked;
            this.initialPrivAllowQuickCampaign = this.chkPrivilegeAllowQuickCampaign.Checked;
            this.initialPrivUseInternetMarketing = this.chkPrivilegeUseInternetMarketing.Checked;
        }

        private void SiteMapControl_Leave(object sender, EventArgs e)
        {
            if (this.initialEntity != this.txtPrivilegeEntity.Text ||
                   this.initialPrivCreate != this.chkPrivilegeCreate.Checked ||
                    this.initialPrivRead != this.chkPrivilegeRead.Checked ||
                    this.initialPrivWrite != this.chkPrivilegeWrite.Checked ||
                    this.initialPrivDelete != this.chkPrivilegeDelete.Checked ||
                    this.initialPrivAppend != this.chkPrivilegeAppend.Checked ||
                    this.initialPrivAppendTo != this.chkPrivilegeAppendTo.Checked ||
                    this.initialPrivShare != this.chkPrivilegeShare.Checked ||
                    this.initialPrivAssign != this.chkPrivilegeAssign.Checked ||
                    this.initialPrivAll != this.chkPrivilegeAll.Checked ||
                    this.initialPrivAllowQuickCampaign != this.chkPrivilegeAllowQuickCampaign.Checked ||
                    this.initialPrivUseInternetMarketing != this.chkPrivilegeUseInternetMarketing.Checked)
            {
                if (MessageBox.Show("You didn't save your changes! Do you want to save them now?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.Save();
                }
            }
        }

        public void Save()
        {
            Dictionary<string, string> collection = new Dictionary<string, string>();

            string privilege = "";

            if (this.chkPrivilegeAll.Checked)
                privilege += "All";
            if (this.chkPrivilegeAllowQuickCampaign.Checked)
                privilege += ",AllowQuickCampaign";
            if (this.chkPrivilegeAppend.Checked)
                privilege += ",Append";
            if (this.chkPrivilegeAppendTo.Checked)
                privilege += ",AppendTo";
            if (this.chkPrivilegeAssign.Checked)
                privilege += ",Assign";
            if (this.chkPrivilegeCreate.Checked)
                privilege += ",Create";
            if (this.chkPrivilegeDelete.Checked)
                privilege += ",Delete";
            if (this.chkPrivilegeRead.Checked)
                privilege += ",Read";
            if (this.chkPrivilegeShare.Checked)
                privilege += ",Share";
            if (this.chkPrivilegeUseInternetMarketing.Checked)
                privilege += ",UseInternetMarketing";
            if (this.chkPrivilegeWrite.Checked)
                privilege += ",Write";

            if (privilege.StartsWith(","))
                privilege = privilege.Remove(0, 1);

            if(this.txtPrivilegeEntity.Text.Length > 0)
                collection.Add("Entity", this.txtPrivilegeEntity.Text);
            if (privilege.Length > 0)
                collection.Add("Privilege", privilege);

            this.initialEntity = this.txtPrivilegeEntity.Text;
            this.initialPrivCreate = this.chkPrivilegeCreate.Checked;
            this.initialPrivRead = this.chkPrivilegeRead.Checked;
            this.initialPrivWrite = this.chkPrivilegeWrite.Checked;
            this.initialPrivDelete = this.chkPrivilegeDelete.Checked;
            this.initialPrivAppend = this.chkPrivilegeAppend.Checked;
            this.initialPrivAppendTo = this.chkPrivilegeAppendTo.Checked;
            this.initialPrivShare = this.chkPrivilegeShare.Checked;
            this.initialPrivAssign = this.chkPrivilegeAssign.Checked;
            this.initialPrivAll = this.chkPrivilegeAll.Checked;
            this.initialPrivAllowQuickCampaign = this.chkPrivilegeAllowQuickCampaign.Checked;
            this.initialPrivUseInternetMarketing = this.chkPrivilegeUseInternetMarketing.Checked;


            this.SendSaveMessage(collection);
        }

        #region Send Events

        /// <summary>
        /// Sends a connection success message 
        /// </summary>
        /// <param name="service">IOrganizationService generated</param>
        /// <param name="parameters">Lsit of parameter</param>
        private void SendSaveMessage(Dictionary<string, string> collection)
        {
            SaveEventArgs sea = new SaveEventArgs() { AttributeCollection = collection };

            if (this.Saved != null)
            {
                this.Saved(this, sea);
            }
        }

        #endregion
    }
}
